<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php'; //include thu vien language
require_once 'CMS/User.php';

//Người tạo : Nguyễn Hồng Danh - Team 2
//Người chỉnh sửa: Nguyễn Huỳnh Đại Thành - Team 3
//Module 	: MostViewNews
//Chức năng	: Show ra những tin tức có người xem nhiều nhất

class MostViewNews_IndexController {

	public function init(){
		parent::init();
	}

	public function indexAction()
	{
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/mostviewnews');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
			
		/*========== Start Get Text ==============*/
			$this->view->textviews 		= $language->get('text_views');
		/*========== End Get Text ==============*/
		
		//Start------Check Input Parameter
		$path = APPLICATION_PATH."/widgets/mostviewnews/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		//End--------Check Input Parameter
		
		//Start------Khai báo các parameter
		$tbThings= new MostViewNews_Model_Things();
		//End--------Khai báo các parameter

		
		//Start------Get MostView
		$MostViewNews=$tbThings->getMostViewThings(2,$xmlconfig->limit);
		$i = 0;
		foreach($MostViewNews as $MostViewNew)
		{
			$title_lang=unserialize($MostViewNew["title"]);
			
			if(isset($title_lang[$name['name']]))
			{
				$MostViewNews[$i]["title"]=$title_lang[$name['name']];
			}
			else
				$MostViewNews[$i]["title"]="------------------";
				$i++;
		}
		//End--------Get MostView
		
		//Start------Truyền parameter qua View
		$titleXML = 'title'.$name['name'];
		if(isset($xmlconfig->$titleXML))
			$this->view->title = $xmlconfig->$titleXML;
		else
        	$this->view->title = $xmlconfig->titleDefault;
		$this->view->AllMostViewNews = $MostViewNews;
		//End--------Truyền parameter qua View
		/************ Start check user login *********/
		$session1 = new Zend_Session_Namespace('login.auth');
		if(isset($session1->id_user))
		{
			$this->view->login = "1";
		
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_widget($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		/************ End check user login *********/
	}
		
}